<template>
  <view>
    <view class="search-box">
      <uni-search-bar @confirm="searchSureFn" @input="input" :radius="100" cancelButton="none"></uni-search-bar>
    </view>
    <!-- 搜索联想 -->
    <view class="search_list" v-if="searchResList.length">
      <view class="search_item" v-for="(item,inx) in searchResList" :key="inx" @click="toDetailFn(item.goods_id)">
        <view class="search_name">
          {{item.goods_name}}
        </view>
        <uni-icons type="arrowright" font-family="iconfont" color="#007AFF" size="16"></uni-icons>
      </view>
    </view>
    <!-- 搜索历史记录 -->
    <view class="search_history" v-else>
      <view class="search_history_title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="16" @click="clearFn"></uni-icons>
      </view>
      <view class="search_history_list">
        <uni-tag :text="item" v-for="(item,inx) in searchHistoryList" :key="inx" @click="toGoodsList(item)"></uni-tag>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        timer: null,
        keyWords: '',
        // 搜索结果列表
        searchResList: [],
        // 搜索历史列表
        searchHistoryList: []
      };
    },
    onLoad() {
      this.searchHistoryList = JSON.parse(uni.getStorageSync('keyWord') || '[]')
    },
    methods: {
      // input事件防抖处理
      input(e) {
        clearTimeout(this.timer)
        this.timer = setTimeout(async() => {
          this.keyWords = e.value
          await this.searchFn()
          this.addHistory(this.keyWords)
        }, 500)
      },
      // 根据关键词进行搜索联想函数
      async searchFn() {
        if (this.keyWords === '') {
          return this.searchResList = []
        }
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/qsearch', {
          query: this.keyWords
        })
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        }
        this.searchResList = res.message
      },
      // 新增历史记录事件
      addHistory(val){
        const inx = this.searchHistoryList.indexOf(val)
        if(inx !== -1) {
          this.searchHistoryList.splice(inx,1)
        }
        this.searchHistoryList.unshift(val)
        // 搜索历史记录持久化保存
        uni.setStorageSync('keyWord',JSON.stringify(this.searchHistoryList))
      },
      // 清空历史记录
      clearFn(){
        this.searchHistoryList = []
        uni.setStorageSync('keyWord','[]')
      },
      // 点击跳转详情页函数
      toDetailFn(id) {
        uni.navigateTo({
          url: `/subpkg/goods_detail/goods_detail?goods_id =${id}`
        })
      },
      // 点击历史记录跳转到商品列表
      toGoodsList(item){
        uni.navigateTo({
          url:`/subpkg/goods_list/goods_list?query=${item}`
        })
      }
    }
  }
</script>

<style lang="scss">
  .search-box {
    position: sticky;
    top: 0;
    z-index: 999;
  }

  // 搜索联想
  .search_list {
    padding: 0 5px;

    .search_item {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 13px 0;
      font-size: 12px;
      border-bottom: 1px solid #efefef;

      .search_name {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
    }
  }

  // 搜索历史记录
  .search_history {
    padding: 0 5px;

    .search_history_title {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 40px;
      font-size: 13px;
    }

    .search_history_list {
      display: flex;

      .uni-tag {
        margin-top: 5px;
        margin-right: 5px;
      }
    }
  }
</style>
